package com.labichaoka.chaoka.utils;

import android.content.Context;

import com.labichaoka.chaoka.base.MyApplication;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;

/**
 * Created by jackwang on 16-10-9.
 */

public class FileUtil {

    public static final String UTF_8 = "UTF-8";

    public static final String readStringFromFileDir(String filename, String charset, boolean aesDecode) {
        try {
            BufferedReader input = null;
            try {
                charset = charset == null ? UTF_8 : charset;
                input = new BufferedReader(new InputStreamReader(MyApplication.mContext.openFileInput(filename), charset),
                        1024 * 16);

                final StringBuffer sb = new StringBuffer();
                String line;
                while ((line = input.readLine()) != null) {
                    sb.append(aesDecode ? AESUtil.decode(line) : line);
                }
                return sb.toString();
            } finally {
                if (input != null) input.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static final String readStringFromInputStream(InputStream inputStream, String charset) {
        try {
            BufferedReader input = null;
            try {
                charset = charset == null ? UTF_8 : charset;
                input = new BufferedReader(new InputStreamReader(inputStream, charset), 1024 * 16);

                final StringBuffer sb = new StringBuffer();
                String line;
                while ((line = input.readLine()) != null) {
                    sb.append(line);
                }
                return sb.toString();
            } finally {
                if (input != null) input.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean writeToFileDir(InputStream in, String toFileName) {
        try {
            BufferedOutputStream output = null;
            BufferedInputStream input = null;
            try {
                input = new BufferedInputStream(in, 1024 * 16);
                final FileOutputStream out = MyApplication.mContext.openFileOutput(toFileName, Context.MODE_PRIVATE);
                output = new BufferedOutputStream(out, 1024 * 16);

                byte[] buf = new byte[1024 * 2];
                int read;
                while ((read = input.read(buf)) != -1) {
                    output.write(buf, 0, read);
                }
                output.flush();
            } finally {
                if (output != null) output.close();
                if (input != null) input.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static boolean writeToFileDir(Reader reader, String toFileName, String charset, boolean aesEncode) {
        try {
            BufferedWriter output = null;
            BufferedReader input = null;
            try {
                charset = charset == null ? UTF_8 : charset;
                input = new BufferedReader(reader, 1024 * 16);
                final FileOutputStream out = MyApplication.mContext.openFileOutput(toFileName, Context.MODE_PRIVATE);
                output = new BufferedWriter(new OutputStreamWriter(out, charset),
                        1024 * 16);

                char[] buf = new char[1024 * 2];
                StringBuffer sb = new StringBuffer();
                int read;
                while ((read = input.read(buf)) != -1) {
                    if (sb.length() > 0)
                        sb.delete(0, sb.length());
                    sb.append(buf, 0, read);

                    output.write(aesEncode ? AESUtil.encode(sb.toString()) : sb.toString());
                    output.newLine();
                }
                output.flush();
            } finally {
                if (output != null) output.close();
                if (input != null) input.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    public static void deleteFiles(String folderPath) {
        File dir = new File(folderPath);
        if (dir == null || !dir.exists() || !dir.isDirectory() || dir.listFiles() == null)
            return;
        for (File file : dir.listFiles()) {
            if (file.isFile())
                file.delete();
        }
    }

    public static String readAssetsByName(Context context, String name,
            String encoding) {
        String text = null;
        InputStreamReader inputReader = null;
        BufferedReader bufReader = null;
        try {
            inputReader = new InputStreamReader(context.getAssets().open(name));
            bufReader = new BufferedReader(inputReader);
            String line = null;
            StringBuffer buffer = new StringBuffer();
            while ((line = bufReader.readLine()) != null) {
                buffer.append(line);
            }
            text = new String(buffer.toString().getBytes(), encoding);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufReader != null) {
                    bufReader.close();
                }
                if (inputReader != null) {
                    inputReader.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return text;
    }
}
